package com.icesoft.system.util;

import com.icesoft.system.auth.entity.LoginUser;
import com.icesoft.system.entity.Account;
import com.icesoft.system.entity.BaseLoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;

@Slf4j
public class SecurityUtils extends org.apache.shiro.SecurityUtils {

	public static <T extends BaseLoginUser> LoginUser<T> getLoginUser() {
		try {
			Subject subject = getSubject();
			return (LoginUser<T>) subject.getPrincipal();
		} catch (Exception e) {
			log.warn(e.getMessage());
			return null;
		}
	}

	public static <T extends BaseLoginUser> T getCurrentUser() {
		try {
			Subject subject = getSubject();
			LoginUser<T> principal = (LoginUser<T>) subject.getPrincipal();
			return principal.getUser();
		} catch (Exception e) {
			log.warn(e.getMessage());
			return null;
		}
	}

	public static <T extends BaseLoginUser> Account getCurrentAccount() {
		try {
			Subject subject = getSubject();
			LoginUser<T> principal = (LoginUser<T>) subject.getPrincipal();
			return principal.getAccount();
		} catch (Exception e) {
			log.warn(e.getMessage());
			return null;
		}
	}

	public static <T extends BaseLoginUser> boolean isAdmin() {
		try {
			Subject subject = getSubject();
			LoginUser<T> principal = (LoginUser<T>) subject.getPrincipal();
			return principal.getUser().getId() == 1L;
		} catch (Exception e) {
			log.warn(e.getMessage());
			return false;
		}
	}
}
